package com.gaswell.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gaswell.common.log.LogAnnotation;
import com.gaswell.entity.Qba02;
import com.gaswell.pojo.RtuData;
import com.gaswell.service.IQba02Service;
import com.gaswell.service.QjService;
import com.gaswell.service.UserService;
import com.gaswell.vo.Qba01Vo;
import com.gaswell.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.*;

/**
 * <p>
 * 采气井月数据 前端控制器
 * </p>
 *
 * @author Lei Wang
 * @since 2022-05-24
 */
@RestController
@RequestMapping("/qba02")
@Api(tags = "采气井月数据 前端控制器")
public class Qba02Controller {
    @Autowired
    private IQba02Service iQba02Service;

    @Autowired
    QjService qjService;
    @Autowired
    private UserService userService;

    @PostMapping("addOne")
    @ApiOperation(value = "添加一条数据")
    @LogAnnotation(module = "采气井月数据", operator = "添加一条数据")
    public Result insertOne(String userName, @RequestBody Qba02 qba02) {
        return iQba02Service.insertOne(qba02);
    }


    @PostMapping("updataOne")
    @ApiOperation(value = "修改一条数据")
    @LogAnnotation(module = "采气井月数据", operator = "修改一条数据")
    public Result updateOne(String userName, @RequestBody Qba02 qba02) {
        return iQba02Service.updateOne(qba02);
    }

    @PostMapping("selectByProperties")
    @ApiOperation(value = "按条件查询（分页）")
    @LogAnnotation(module = "采气井月数据", operator = "按条件查询")
    public Result selectList(String userName, @RequestBody Qba02 qba02, int sort) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        int department = userService.selectUserByuserName(userName).getUser_department_id();
        return iQba02Service.selectProperties(qba02, sort, department);
    }

    @PostMapping("selectAll")
    @ApiOperation(value = "查询所有数据（分页）")
    @LogAnnotation(module = "采气井月数据", operator = "查询所有数据")
    public Result selectAll(String userName, int current, int size, String jh, int sort) {
        return iQba02Service.selectAll(current, size, jh, sort);
    }

    @PostMapping("selectByMutiProperties")
    @ApiOperation(value = "按条件查询(多选)")
    @LogAnnotation(module = "采气井月数据", operator = "按条件查询（多选）")
    public Result selectData(String userName, @RequestBody Qba01Vo qba01Vo, String date, int sort) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        int department = userService.selectUserByuserName(userName).getUser_department_id();
        Result result = iQba02Service.selectByMutiProperties(qba01Vo, date, sort, department);
        return result;
    }

    @PostMapping("finZyqm")
    @ApiOperation(value = "查询所有作业区名")
    @LogAnnotation(module = "采气井月数据", operator = "查询所有区名")
    public Result findQm(String userName) {
        return iQba02Service.findZyqm();
    }

    @PostMapping("findZm")
    @ApiOperation(value = "查询所有站名")
    @LogAnnotation(module = "采气井月数据", operator = "查询所有站名")
    public Result findZm(String userName) {
        return iQba02Service.findZm();
    }

    @PostMapping("findDm")
    @ApiOperation(value = "查询所有队名")
    @LogAnnotation(module = "采气井月数据", operator = "查询所有队名")
    public Result findDm(String userName) {
        return iQba02Service.findDm();
    }

    @PostMapping("findQkmc")
    @ApiOperation(value = "查询所有区块名称")
    @LogAnnotation(module = "采气井日数据", operator = "查询所有区块名称")
    public Result selectAll(String userName) {
        return iQba02Service.findQkmc();
    }

    @GetMapping("outExcel")
    @ApiOperation(value = "导出为excel")
    @LogAnnotation(module = "采气井月数据", operator = "导出为excel")
    public void outExcel(String userName, HttpServletResponse response, String[] jhs, String start, String end, String type, String[] zyqs) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("采气井月数据", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileName + ".xlsx");
        ExcelWriterBuilder workbook = EasyExcel.write(response.getOutputStream(), Qba02.class);
        ExcelWriterSheetBuilder sheet = workbook.sheet();
        int department = userService.selectUserByuserName(userName).getUser_department_id();
        LambdaQueryWrapper<Qba02> rtuDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<String> lists = new LinkedList<>();
        if (department == 2) {
            lists = qjService.findJhByYm("川渝");
        }
        if (department == 3) {
            lists = qjService.findJhByYm("大庆");
        }

        if (StringUtils.isNotBlank(end)) {
            rtuDataLambdaQueryWrapper.le(Qba02::getNy, end);
        }
        if (StringUtils.isNotBlank(start)) {
            rtuDataLambdaQueryWrapper.ge(Qba02::getNy, start);
        }
        if (type != null && type.equals("zyq")) {

            if (lists != null && lists.size() > 0) {
                rtuDataLambdaQueryWrapper.in(Qba02::getJh, lists);
                if (zyqs != null && zyqs.length > 0)
                    rtuDataLambdaQueryWrapper.in(Qba02::getQm, zyqs);
            }
        } else {
            if (lists != null && lists.size() > 0) {
                rtuDataLambdaQueryWrapper.in(Qba02::getJh, lists);
                if (jhs != null && jhs.length > 0)
                    rtuDataLambdaQueryWrapper.in(Qba02::getJh, jhs);
            }
        }

        List<Qba02> list = iQba02Service.list(rtuDataLambdaQueryWrapper);
        System.out.println(list);
        sheet.doWrite(list);
    }

}
